package com.example.sbbsec;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.newLinkedList;

/**
 * Created by Arat.Wu on 2018/12/04.
 */
public class completableMethods5 {



    public static void main(String[] args) {
        final List<City> cities = newArrayList();
        cities.add(City.builder().id(1L).name("太仓").info("太仓信息").build());
        cities.add(City.builder().id(2L).name("沙溪").build());

        final List<City> cities1 = Arrays.asList(City.builder().id(1L).name("太仓").build(),
                City.builder().id(2L).name("沙溪").info("沙溪信息").build(),
                City.builder().id(3L).name("浏河").info("浏河信息").build());

        List<Integer> list3 = Arrays.asList(64, 49, 25, 16, 9, 4, 1, 81, 36, 36);
        List<Integer> list4 = newArrayList();
        Map<String,Integer> stringIntegerMap = Maps.newConcurrentMap();
        final List<Integer> collect = IntStream.range(1, 1000000).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        Long start =System.currentTimeMillis();
        CompletableFuture.runAsync(()->
            collect.parallelStream().forEach(item->
                    CompletableFuture.runAsync(()->stringIntegerMap.merge("count",item,(v1,v2)->v1+v2)).join()
            )).join();
        Long time=System.currentTimeMillis()-start;
        System.out.println(time+":"+stringIntegerMap);

        stringIntegerMap.remove("count");
        Long start2 =System.currentTimeMillis();

                collect.forEach(item->stringIntegerMap.merge("count",item,(v1,v2)->v1+v2));

        Long time2=System.currentTimeMillis()-start2;
        System.out.println(time2+":"+stringIntegerMap);
       // CompletableFuture.runAsync()
    }


}
